package com.marvels.loanapply.api.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.BasicDataDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.loanapply.api.LoanBasicDataApi;
import com.marvels.loanapply.dao.LoanBasicDataDao;
import com.marvels.loanapply.dto.LoanInvestmentInfoDTO;

@Service
public class LoanBasicDataApiImpl implements LoanBasicDataApi {
	@Autowired
	private LoanBasicDataDao loanBasicDataDao;

	@Override
	public List<BasicDataDTO> getPurposeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.PURPOSE_LIST);
//		MarvelsLogUtil.getInstance().info("从redis缓存获取数据。。。。。。。。。。。。。。。");
		List<BasicDataDTO> list = null;
//		if(list == null){
//			MarvelsLogUtil.getInstance().info("redis缓存中没有数据，从服务获取。。。。。。。。。。。。。。。");
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_PURPOSE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.PURPOSE_LIST, list);
			MarvelsLogUtil.getInstance().info("将服务数据缓存到redis。。。。。。。。。。。。。。。");
//		}
		return list;
	}
	
	private List<BasicDataDTO> getBasicDataList(String tableName){
		List<String> columns = new ArrayList<String>();
		columns.add(MarvelsConstant.COLUMN_NAME_CODE);
		columns.add(MarvelsConstant.COLUMN_NAME_NAME);
		return loanBasicDataDao.selectBasicDataList(columns, tableName);
	}
	
	/**
	 * 从redis缓存获取基表数据
	 * @param key
	 * @return
	 */
	private List<BasicDataDTO> getCacheList(String key){
		List<BasicDataDTO> list = null;
		Object o = CacheUtils.getCacheObject(key);
		if(o != null){
			list = (List<BasicDataDTO>)o;
		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getPaymentModeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.PAYMENT_MODE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_PAYMENT_MODE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.PAYMENT_MODE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getLifeInsuranceTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.LIFE_INSURANCE_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_LIFE_INSURANCE_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.LIFE_INSURANCE_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getInsuranceTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.INSURANCE_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_INSURANCE_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.INSURANCE_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getInsuranceStatusList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.INSURANCE_STATUS_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_INSURANCE_STATUS_INFO);
			CacheUtils.cacheObject(MarvelsConstant.INSURANCE_STATUS_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getInsuranceCompanyList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.INSURANCE_COMPANY_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_INSURANCE_COMPANY_INFO);
			CacheUtils.cacheObject(MarvelsConstant.INSURANCE_COMPANY_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getEmployTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.EMPLOY_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_EMPLOY_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.EMPLOY_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getMaritalStatusList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.MARITAL_STATUS_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_MARITAL_STATUS_INFO);
			CacheUtils.cacheObject(MarvelsConstant.MARITAL_STATUS_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getEducationLevelList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.EDUCATION_LEVEL_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_EDUCATION_LEVEL_INFO);
			CacheUtils.cacheObject(MarvelsConstant.EDUCATION_LEVEL_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getLiveTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.LIVE_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_LIVE_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.LIVE_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getProvinceList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.PROVINCE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_PROVINCE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.PROVINCE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getCityList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.CITY_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_CITY_INFO);
			CacheUtils.cacheObject(MarvelsConstant.CITY_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getRegionList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.REGION_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_REGION_INFO);
			CacheUtils.cacheObject(MarvelsConstant.REGION_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getCompanyTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.COMPANY_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_COMPANY_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.COMPANY_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getWorkTimeTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.WORKTIME_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_WORK_TIME_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.WORKTIME_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getIncomeTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.INCOME_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_INCOME_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.INCOME_TYPE_LIST, list);
//		}
		return list;
	}

//	@Override
//	public List<BasicDataDTO> getRelationList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.RELATION_LIST);
//		if(list == null){
//			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_RELATION_INFO);
//			CacheUtils.cacheObject(MarvelsConstant.RELATION_LIST, list);
//		}
//		return list;
//	}
	
	@Override
	public List<BasicDataDTO> getRelationList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.RELATION_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			List<String> columns = new ArrayList<String>();
			columns.add(MarvelsConstant.COLUMN_NAME_CODE);
			columns.add(MarvelsConstant.COLUMN_NAME_NAME);
			columns.add(MarvelsConstant.COLUMN_NAME_TYPE+" as attr1");
			list = loanBasicDataDao.selectBasicDataList(columns, MarvelsConstant.TABLE_NAME_MCLA_RELATION_INFO);
			CacheUtils.cacheObject(MarvelsConstant.RELATION_LIST, list);
//		}
		return list;
	}
	

	@Override
	public List<BasicDataDTO> getRelationOtherList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.RELATION_OTHER_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_RELATION_OTHER_INFO);
			CacheUtils.cacheObject(MarvelsConstant.RELATION_OTHER_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getPhoneCheckResultList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.PHONE_CHK_RESULT_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_PHONE_CHK_RESULT_INFO);
			CacheUtils.cacheObject(MarvelsConstant.PHONE_CHK_RESULT_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getEnterpriseScaleTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.ENTERPRISE_SCALE_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_ENTERPRISE_SCALE_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.ENTERPRISE_SCALE_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getTwoCareerTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.TWO_CAREER_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_TWO_CAREER_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.TWO_CAREER_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getThreeCareerTypeList() throws MarvelsBusinessException {
//		List<BasicDataDTO> list = getCacheList(MarvelsConstant.THREE_CAREER_TYPE_LIST);
		List<BasicDataDTO> list = null;
//		if(list == null){
			list = getBasicDataList(MarvelsConstant.TABLE_NAME_MCLA_THREE_CAREER_TYPE_INFO);
			CacheUtils.cacheObject(MarvelsConstant.THREE_CAREER_TYPE_LIST, list);
//		}
		return list;
	}

	@Override
	public List<BasicDataDTO> getloanInvestmentList() throws MarvelsBusinessException {
		List<LoanInvestmentInfoDTO> list = loanBasicDataDao.selectLoanInvestmentList();
		List<LoanInvestmentInfoDTO> parent = new ArrayList<LoanInvestmentInfoDTO>();
		Map<String,List<LoanInvestmentInfoDTO>> map = new HashMap<String,List<LoanInvestmentInfoDTO>>();
		for(int i=0;i<list.size();i++){
			LoanInvestmentInfoDTO o = list.get(i);
			if(StringUtils.equals("-1", o.getParentCode())){
				List<LoanInvestmentInfoDTO> children = new ArrayList<LoanInvestmentInfoDTO>();
				parent.add(o);
				for(int j=0;j<list.size();j++){
					LoanInvestmentInfoDTO b = list.get(j);
					if(StringUtils.equals(o.getCode(), b.getParentCode())){
						children.add(b);
					}
				}
				map.put(o.getCode(), children);
			}
		}
		System.out.println(JSON.toJSONString(parent));
		System.out.println(JSON.toJSONString(map));
		return null;
	}
	
	

}
